//
//  WCNotifyMessageListVC.m
//  WC
//
//  Created by wsj2015 on 16/1/21.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNotifyMessageListVC.h"
#import "MJRefresh.h"
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
#import "WCDefaultManager.h"
#import "WCNotifyMessageCell.h"
#import "WCNotifyMessageModel.h"
#import "UITableView+FDTemplateLayoutCell.h"
//#import "WCOrderDetailViewController.h"
#import "UITableView+(removeMjrefreshFooter).h"
#import "WCNOrderDetailVC.h"
#import "WCNMyMessageVC.h"
#import "WCInputMessageVC.h"
#import "WCBPushToolByWsj.h"
#import "WCNAllowBeNursingPeopleVC.h"
#import "WCNJoinMeetingVC.h"
@interface WCNotifyMessageListVC ()

{
    NSInteger _page;
    NSInteger _messageType;

}
@property (nonatomic, retain) NSMutableArray *dataArray;
@end

@implementation WCNotifyMessageListVC
- (instancetype)initWithMessageType:(NSInteger)messageType {
    self = [super init];
    if (self) {
        _messageType = messageType;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    [self getMessageListIsRecover:YES];
    
    
}


- (void)configUI{
    _page = 1;
   
   self.title = @"消息列表";
    self.tableView.backgroundColor = kAppBg;
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNotifyMessageCell" bundle:nil] forCellReuseIdentifier:@"notifyMessageCellId"];
    weakSelf_define;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        _page =1;
        [weakSelf getMessageListIsRecover:YES];
        
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        _page ++;
         [weakSelf getMessageListIsRecover:NO];
    }];


}
- (NSMutableArray *)dataArray{
    if (_dataArray ==nil) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WCNotifyMessageCell *cell =[WCNotifyMessageCell cellForTableView:tableView indexPath:indexPath];
    cell.model =self.dataArray[indexPath.row];
    [cell setNeedsUpdateConstraints];
    
    [cell updateConstraintsIfNeeded];
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    CGFloat h =[tableView fd_heightForCellWithIdentifier:@"notifyMessageCellId" configuration:^(WCNotifyMessageCell *cell) {
         cell.model =self.dataArray[indexPath.row];
    }];
    return h;

    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNotifyMessageModel *model =self.dataArray[indexPath.row];
    
    if ([model.messageType integerValue] ==1) {
        WCNOrderDetailVC *vc =[[WCNOrderDetailVC alloc] init];
        vc.orderId = model.sourceId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if([model.messageType integerValue] ==10) { //留言提醒
        WCInputMessageVC *vc =[[WCInputMessageVC alloc] init];
        vc.messageId = [model.sourceId integerValue];
        vc.isNeedCheck = YES;
        [self.navigationController pushViewController:vc animated:YES];
    
    }else if ([model.messageType integerValue] ==20){
        if ([model.messageStatus integerValue]==10) {
            return;
        }
        if([model.messageStatus integerValue] ==201 ||[model.messageStatus integerValue] ==202){
            WCNAllowBeNursingPeopleVC *vc =[WCNAllowBeNursingPeopleVC new];
            vc.nursingPeopleId =[model.sourceId integerValue];
            vc.name = model.messageContent;
            vc.messageId = [model.id integerValue];
            vc.handleInfor = [model.messageType integerValue];
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        WCNAllowBeNursingPeopleVC *vc =[WCNAllowBeNursingPeopleVC new];
        vc.nursingPeopleId =[model.sourceId integerValue];
        vc.messageId = [model.id integerValue];
        vc.name = model.messageContent;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.messageType integerValue] ==4){
        
        //跳转到加入会议页面
        WCNJoinMeetingVC *meetingVC = [WCNJoinMeetingVC new];
        
        NSString *content = model.messageContent;
        NSArray *array = [content componentsSeparatedByString:@":"];
        NSString *meetingID = [array lastObject];
        //会议id
        meetingVC.meetingID = meetingID;
        [self.navigationController pushViewController:meetingVC animated:YES];
    }

   
    if ([model.messageStatus integerValue]==1) {
        [self remarkMessageReadwithMessageId:[model.id integerValue] with:indexPath];
    }
    
    
    
}

- (void)remarkMessageReadwithMessageId:(NSInteger )messageId with:(NSIndexPath *)indexPath{
    
   
    defineWeakSelf;
   
    [[WCNetworkAccessManager sharedMg] POST:kMarkNotityMessage parameters:@{@"messageId":@(messageId)} success:^(NSDictionary *responseObject) {
         [WCBPushToolByWsj saveNewBadge:[WCBPushToolByWsj getLatestBadge]-1];
       
        WCNotifyMessageModel *model = weakSelf.dataArray[indexPath.row];
        model.messageStatus =[NSString stringWithFormat:@"%@",@(2)];
        [weakSelf.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationBottom];
//        [CoreSVP showSVPWithType:CoreSVPTypeBottomMsg Msg:responseObject[@"data"] duration:1 allowEdit:YES beginBlock:nil completeBlock:nil];
    } failure:nil netError:nil always:^{
        
    }];

}

#pragma mark -请求数据
- (void)getMessageListIsRecover:(BOOL)isRecover{
    //page:当前页 (默认1)
    //rows:一页显示数量(默认10)
    //targetId:接收消息人ID(必填)
    //targetType:接收消息人类型(必填 1.护士 2.患者)
    NSMutableDictionary *params =[NSMutableDictionary new];
    params[@"page"] =@(_page);
    params[@"rows"] =@(10);
    params[@"targetId"] =@([[[[WCDefaultManager shareMg] account] stringId] integerValue]);
    params[@"targetType"] =@(1);

    [WCPopupLayerManager showLodingView];
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] POST:kNotifyMessageList parameters:params success:^(NSDictionary  *responseObject) {
        NSDictionary *dataDict =responseObject[@"data"];
        
        [UITableView remvoMJRefreshFooter:_page totalPage:[dataDict[@"totalPage"] integerValue] forTableView:weakSelf.tableView withFootFreshBlock:^{
            _page ++;
            [weakSelf getMessageListIsRecover:NO];
        }];
        
        if (isRecover) {
            [weakSelf.dataArray removeAllObjects];
        }
        
        for (NSDictionary *dict in dataDict[@"result"]) {
            WCNotifyMessageModel *model =[[WCNotifyMessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [weakSelf.dataArray addObject:model];
        }
        [weakSelf.tableView reloadData];
    } failure:nil netError:nil always:^{
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        [WCPopupLayerManager dissmissLodingView];
    }];
    
    


}
@end
